package com.hongkuncheng.cms.controller.admin;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.LockMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.stereotype.Controller;
import com.hongkuncheng.cms.controller.BaseController;
import com.hongkuncheng.cms.helper.PageHelper;
import com.hongkuncheng.cms.model.Friendlink;
import com.hongkuncheng.cms.model.FriendlinkType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 友情链接
 *
 * @author 洪坤成
 */
@Controller
@Transactional
@RequestMapping("admin/friendlink")
public class AfriendlinkController extends BaseController {

    // 友情链接列表
    @RequestMapping
    public String index() {
        Criteria criteriaFriendlink = factorySession.createCriteria(Friendlink.class);
        int friendlinktypeId = request.getParameter("friendlinktypeid") == null ? 0 :
                Integer.parseInt(request.getParameter("friendlinktypeid"));
        if (friendlinktypeId > 0) criteriaFriendlink.add(Restrictions.eq(
                "friendlinkType.id", friendlinktypeId));
        request.setAttribute("friendlinktypeid", friendlinktypeId);
        List<Friendlink> friendlinks = criteriaFriendlink.addOrder(Order.asc("sort")).list();
        request.setAttribute("friendlinks", friendlinks);
        List<FriendlinkType> friendlinktypes = factorySession.createCriteria(FriendlinkType.class)
                .list();
        request.setAttribute("friendlinktypes", friendlinktypes);
        return view();
    }

    // 友情链接编辑
    @RequestMapping("edit")
    public String edit() {
        int friendlinktypeId = Integer.parseInt(request.getParameter("friendlinktypeid"));
        int friendlinkId = request.getParameter("friendlinkid") == null ? 0 :
                Integer.parseInt(request.getParameter("friendlinkid"));
        Friendlink friendlink = friendlinkId == 0 ? new Friendlink() : factorySession.get(Friendlink.class, friendlinkId);
        request.setAttribute("friendlink", friendlink);
        request.setAttribute("friendlinktypeid", friendlinktypeId);
        List<FriendlinkType> friendlinktypes = factorySession.createCriteria(FriendlinkType.class).list();
        request.setAttribute("friendlinktypes", friendlinktypes);
        return view();
    }

    // 友情链接编辑处理
    @RequestMapping("editdo")
    @ResponseBody
    public void editdo() {
        checkAdminPermission("yunwei_plugin_frendlink");
        int friendlinktypeId = Integer.parseInt(request.getParameter("friendlinktypeid"));
        int friendlinkId = Integer.parseInt(request.getParameter("friendlinkid"));
        Friendlink friendlink = friendlinkId == 0 ? new Friendlink() :
                factorySession.load(Friendlink.class, friendlinkId, LockMode.PESSIMISTIC_WRITE);
        FriendlinkType friendlinktype = factorySession.load(FriendlinkType.class, friendlinktypeId);
        friendlink.setFriendlinkType(friendlinktype);
        friendlink.setName(request.getParameter("name"));
        friendlink.setUrl(request.getParameter("url"));
        friendlink.setImage(request.getParameter("image"));
        friendlink.setSort(Integer.parseInt(request.getParameter("sort")));
        if (friendlinkId == 0) factorySession.save(friendlink);
        recordLog((friendlinkId == 0 ? "新建" : "更新") + "友情链接《" + friendlink.getName().trim() + "》");
        PageHelper.alertAndRedirect("保存成功", "./?friendlinktypeid=" + friendlinktypeId);
    }

    // 友情链接删除
    @RequestMapping("delete")
    @ResponseBody
    public void delete() {
        checkAdminPermission("yunwei_plugin_frendlink");
        int friendlinktypeId = Integer.parseInt(request.getParameter("friendlinktypeid"));
        int friendlinkId = Integer.parseInt(request.getParameter("friendlinkid"));
        Friendlink friendlink = factorySession.load(Friendlink.class, friendlinkId, LockMode.PESSIMISTIC_WRITE);
        factorySession.delete(friendlink);
        recordLog("删除友情链接《" + friendlink.getName().trim() + "》");
        PageHelper.alertAndRedirect("删除成功", "./?friendlinktypeid=" + friendlinktypeId);
    }

}
